// pages/goods/goods.js
import { delshop, dqgoods, numbers } from '../../utils/http'
Page({
    data: {
        shop: [],
        shops:null,
        show: true,
        price: null,
    },
    onLoad: function (options) {
        console.log(options.list)
    
        this.getlist()
        if (options.list == null) {
            let res = JSON.stringify(options.list)
        } else { 
            let res = JSON.parse(options.list)
            console.log(res)
            this.setData({ shop: res.items})
        }
    },
    // 请求购物车数据
    getlist(){
        let token = wx.getStorageSync('token')
        dqgoods(token).then(res => {
            // console.log(res.data)
            this.setData({ shop: res.data.items, price: res.data.price })
        })
    },
    //   加减
    onChange(e) {
        console.log(e)
        let token = wx.getStorageSync('token')
        let key = e.currentTarget.dataset.key
        let number = e.detail
        numbers(key, token, number).then(res => {
            // console.log(res)
            this.setData({ number: res.data.number, price: res.data.price })
        })
    },
    //删除
    del(e) {
        let key = e.currentTarget.dataset.key
        let index = e.currentTarget.dataset.index
        let token = wx.getStorageSync('token')
        console.log(index )
        delshop(key, token).then(res => {
            console.log(res)
            if(this.data.shop.length<=1){
                this.setData({shop:[]})
            }
            this.getlist()
        })
    },
    jumps(e){
        let id=e.currentTarget.dataset.id
        wx.reLaunch({
          url: '/pages/productdetails/productdetails?id='+id,
        })

    }
})